{% extends 'base/base.html' %}
{% load static %}
{% load humanize %}
{% load permission_tags %}

{% block title %}
Scan history
{% endblock title %}

{% block custom_js_css_link %}
{% endblock custom_js_css_link %}

{% block breadcrumb_title %}
<li class="breadcrumb-item active" aria-current="page">Scan History</li>
{% endblock breadcrumb_title %}

{% block page_title %}
Quick Scan History
{% endblock page_title %}

{% block main_content %}
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="p-2">
        <div class="row">
          <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
            <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="filterMenu">
              Filter <i class="fe-filter"></i>
            </button>
            <div id="filteringText" class="mt-2">
            </div>
            <div class="dropdown-menu" style="width: 30%">
              <div class="px-4 py-3">
                <h4 class="headline-title">Filters</h4>
                <div class="">
                  <label for="filterByOrganization" class="form-label">Filter by Organization</label>
                  <select class="form-control" id="filterByOrganization">
                  </select>
                </div>
                <div class="">
                  <label for="filterByTarget" class="form-label">Filter by Targets</label>
                  <select class="form-control" id="filterByTarget">
                  </select>
                </div>
                <div class="">
                  <label for="filterByScanType" class="form-label">Filter by Scan Type</label>
                  <select class="form-control" id="filterByScanType">
                  </select>
                </div>
                <div class="">
                  <label for="filterByScanStatus" class="form-label">Filter by Scan Status</label>
                  <select class="form-control" id="filterByScanStatus">
                  </select>
                </div>
              </div>
              <div class="dropdown-divider"></div>
              <a href="#" class="dropdown-ite text-primary float-end" id="resetFilters">Reset Filters</a>
            </div>
          </div>
          {% if user|can:'initiate_scans_subscans' %}
          <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
            <a class="btn btn-soft-danger float-end disabled ms-1 mt-1" href="#" onclick="deleteMultipleScan()" id="delete_multiple_button">Delete Multiple Scans</a>
            <a class="btn btn-soft-danger float-end disabled ms-1 mt-1" href="#" onclick="stopMultipleScan()" id="stop_multiple_button">Stop Multiple Scans</a>
          </div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-12">
    <div class="card">
      <form method="POST" id="scan_history_form">
        {% csrf_token %}
        <table id="scan_history_table" class="table dt-responsive w-100">
          <thead>
            <tr>
              <th class="checkbox-column text-center">Serial Number</th>
              <th class="text-center">Serial Number</th>
              <th class="">Domain Name</th>
              <th>Summary</th>
              <th class="">Scan Engine Used</th>
              <th>Last Scan</th>
              <th>Initiated By</th>
              <th class="text-center">Status</th>
              <th class="text-center">Progress</th>
              <th class="text-center no-sorting">Action</th>
            </tr>
          </thead>
          <tbody>
            {% for scan_history in scan_history.all %}
            <tr>
              <td class="checkbox-column"> {{ scan_history.id }} </td>
              <td class=""> {{ scan_history.id }} </td>
              <td class="">
                {{ scan_history.domain.name }}
                <br>
                {% if scan_history.cfg_starting_point_path %}
                  <span><i class=""></i>Path: <code>{{scan_history.cfg_starting_point_path}}</code></span>
                {% endif %}
                <br>
                {% for organization in scan_history.domain.get_organization %}
                <span class="badge badge-soft-dark mt-1 me-1" data-toggle="tooltip" data-placement="top" title="Domain {{domain.name}} belongs to organization {{organization.name}}">{{ organization.name }}</span>
                {% endfor %}
              </td>
              <td class="text-left">
                <span class="badge badge-pills bg-info mt-1" data-toggle="tooltip" data-placement="top" title="Subdomains">{{scan_history.get_subdomain_count}}</span>
                <span class="badge badge-pills bg-warning mt-1" data-toggle="tooltip" data-placement="top" title="Endpoints">{{scan_history.get_endpoint_count}}</span>
                <span class="badge badge-pills bg-danger mt-1" data-toggle="tooltip" data-placement="top" title="{{scan_history.get_critical_vulnerability_count}} Critical, {{scan_history.get_high_vulnerability_count}} High, {{scan_history.get_medium_vulnerability_count}} Medium Vulnerabilities">{{scan_history.get_vulnerability_count}}</span>
              </td>
              <td class="">
                <span class="badge badge-soft-primary">{{ scan_history.scan_type }}</span>
              </td>
              <td>
                <span data-toggle="tooltip" data-placement="top" title="{{scan_history.start_scan_date}}">{{scan_history.start_scan_date|naturaltime}}</span>
              </td>
              <td>
                <span class="badge badge-soft-secondary">{{scan_history.initiated_by.username}}</span>
              </td>
              <td class="text-center">
                {% if scan_history.scan_status == -1 %}
                <span class="badge badge-soft-warning" data-placement="top" data-toggle="tooltip" data-placement="top" title="Waiting for other scans to complete"><span class="spinner-border spinner-border-sm"></span> Pending</span>
                {% elif scan_history.scan_status == 0 %}
                <span class="badge badge-soft-danger">Failed</span>
                {% if scan_history.error_message %}</br><p class="text-danger">Scan Failed due to: {{scan_history.error_message}}</p>{% endif %}
                {% elif scan_history.scan_status == 1 %}
                <span class="badge badge-soft-info"><span class="spinner-border spinner-border-sm"></span> In Progress</span>
                {% elif scan_history.scan_status == 2 %}
                <span class="badge badge-soft-success">Successful</span>
                {% elif scan_history.scan_status == 3 %}
                <span class="badge badge-soft-danger">Aborted</span>
                <br>
                <small class="text-muted">Aborted by: {{scan_history.aborted_by}}</small>
                {% else %}
                <span class="badge badge-soft-danger">Unknown</span>
                {% endif %}
              </td>
              <td class="text-center">
                {% if scan_history.scan_status == -1 %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-warning" role="progressbar" style="width: 75%" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
                {% elif scan_history.scan_status == 0 %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-danger" role="progressbar" style="width: {% widthratio scan_history.scanactivity_set.all|length scan_history.scan_type.get_number_of_steps|add:4  100 %}%"
                  aria-valuemin="0" aria-valuemax="4"></div>
                </div>
                {% elif scan_history.scan_status == 1 %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-primary progress-bar-striped progress-bar-animated" role="progressbar" style="width: {% widthratio scan_history.scanactivity_set.all|length scan_history.scan_type.get_number_of_steps|add:4  100 %}%"
                  aria-valuemin="0" aria-valuemax="4"></div>
                </div>
                {% elif scan_history.scan_status == 2 %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-success" role="progressbar" style="width: 100%" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
                {% elif scan_history.scan_status == 3 %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-danger progress-bar-striped" role="progressbar" style="width: {% widthratio scan_history.scanactivity_set.all|length scan_history.scan_type.get_number_of_steps|add:4  100 %}%" aria-valuemin="0"
                  aria-valuemax="4"></div>
                </div>
                {% else %}
                <div class="progress progress-md mt-1">
                  <div class="progress-bar bg-danger" role="progressbar" style="width: 100%" aria-valuemin="0" aria-valuemax="100">
                  </div>
                </div>
                {% endif %}
              </td>
              <td class="text-center">
                <div class="btn-group mb-2 dropstart">
                  <div class="btn-group">
                    <a href="/scan/{{current_project.slug}}/detail/{{scan_history.id}}" class="btn btn-soft-primary">View Results</a>
                    <div class="btn-group dropstart" role="group">
                      <button type="button" class="btn btn-soft-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="mdi mdi-chevron-right"></i>
                      </button>
                      <div class="dropdown-menu" style="">
                        <a class="dropdown-item text-primary" href="javascript:show_scan_configuration('{{scan_history.cfg_starting_point_path}}', {{scan_history.cfg_out_of_scope_subdomains}}, {{scan_history.cfg_excluded_paths}}, {{scan_history.cfg_imported_subdomains}})"><i class="fe-settings"></i>&nbsp;Show Configurations</a>
                        {% if user|can:'initiate_scans_subscans' %}
                          {% if scan_history.scan_status == 0 or scan_history.scan_status == 2 or scan_history.scan_status == 3 %}
                          <a class="dropdown-item text-primary" href="{% url 'start_scan' current_project.slug scan_history.domain.id %}?rescan=true&history_id={{ scan_history.id }}">
                            <i class="fe-refresh-ccw"></i>&nbsp;Rescan
                          </a>
                          {% endif %}

                          {% if scan_history.scan_status == 1 or scan_history.scan_status == -1%}
                          <a href="#" class="dropdown-item text-danger" onclick="stop_scan(scan_id={{ scan_history.id }}, subscan_id=null, reload_scan_bar=false, reload_location=true)">
                          <i class="fe-alert-triangle"></i>&nbsp;Stop Scan</a>
                          {% endif %}
                        {% endif %}
                        {% if user|can:'modify_scan_results' %}
                          {% if scan_history.scan_status == 2 or scan_history.scan_status == 3 or scan_history.scan_status == 0 %}
                            <a href="#" class="dropdown-item text-danger" onclick="delete_scan('{{ scan_history.id }}')">
                            <i class="fe-trash-2"></i>&nbsp;Delete Scan Results</a>
                          {% endif %}
                          <div class="dropdown-divider"></div>
                        {% endif %}
                        {% if scan.scan_status != -1%}
                          <a href="#" class="dropdown-item text-dark" onclick="initiate_report(id={{scan_history.id}}, has_subdomain_scan={% if 'subdomain_discovery' in scan_history.scan_type.tasks %}true{% else %}false{% endif %}, has_vulnerability_scan={% if 'vulnerability_scan' in scan_history.scan_type.tasks %}true{% else %}false{% endif %}, domain_name='{{ scan_history.domain.name }}')">
                          <i class="fe-download"></i>&nbsp;Scan Report</a>
                        {% endif %}
                        </div>
                      </div>
                    </div>
                  </div>
              </td>
            </tr>
          {% endfor %}
          </tbody>
        </table>
      </form>
    </div>
  </div>
</div>
<div class="modal fade" id="generateReportModal" tabindex="-1" aria-labelledby="modalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg modal-dialog-centered">
    <div class="modal-content border-0 shadow">
      <div class="modal-header bg-light border-bottom-0">
        <h5 class="modal-title" id="modalLabel">Generate Report</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="container-fluid">
          <h6 class="text-primary mb-3">Select Report Type</h6>
          <div class="row g-3 mb-4">
            <div class="col-md-4">
              <div class="card h-100 border-0 shadow-sm">
                <div class="card-body">
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="reportType" id="report_type_checkbox" value="full" checked>
                      <label class="form-check-label w-100" for="fullScan">
                      <h6 class="mb-2">Full Scan</h6>
                      <p class="text-muted small mb-0">Comprehensive analysis including Recon and Vulnerabilities</p>
                    </label>
                  </div>
                </div>
                <div class="card-footer border-0 text-primary bg-soft-primary"> Complete Overview </div>
              </div>
            </div>
            <div class="col-md-4">
              <div class="card h-100 border-0 shadow-sm">
                <div class="card-body">
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="reportType" id="report_type_checkbox" value="recon" disabled>
                    <label class="form-check-label w-100" for="reconReport">
                      <h6 class="mb-2">Reconnaissance</h6>
                      <p class="text-muted small mb-0">Report containing detailed overview of discovered assets only</p>
                    </label>
                  </div>
                </div>
                <div class="card-footer border-0 text-info bg-soft-info"> Asset Mapping </div>
              </div>
            </div>
            <div class="col-md-4">
              <div class="card h-100 border-0 shadow-sm">
                <div class="card-body">
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="reportType" id="report_type_checkbox" value="vuln" disabled>
                    <label class="form-check-label w-100" for="vulnReport">
                      <h6 class="mb-2">Vulnerability</h6>
                      <p class="text-muted small mb-0">Report focused on the identified vulnerabilities and remediation</p>
                    </label>
                  </div>
                </div>
                <div class="card-footer border-0 text-danger bg-soft-danger"> Risk Assessment </div>
              </div>
            </div>
          </div>
          <div class="row g-3">
            <div class="col-md-6">
              <h6 class="text-primary mb-3">Report Template</h6>
              <select id="templateSelect" class="form-select" aria-label="Select report template">
                <option value="modern">Modern Template</option>
                <option value="default">Default Template</option>
              </select>
            </div>
            <div class="col-md-6">
              <h6 class="text-primary mb-3">Additional Options</h6>
              <div class="form-check form-switch" id="report_info_vuln_div">
                <input class="form-check-input" type="checkbox" id="excludeInfoFindings" name="excludeInfoFindings" checked>
                <label class="form-check-label" for="excludeInfoFindings"> Exclude Informational Findings </label>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer bg-light border-top-0">
        <div class="container-fluid">
          <div class="row justify-content-end">
            <div class="col-auto">
              <button type="button" class="btn btn-outline-primary me-2" id="previewReportBtn">Preview Report</button>
              <button type="button" class="btn btn-primary" id="generateReportBtn">Download Report</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock main_content %}


{% block page_level_script %}
<script>
  $(document).ready(function() {
    var table = $('#scan_history_table').DataTable({
      headerCallback: function(e, a, t, n, s) {
        e.getElementsByTagName("th")[0].innerHTML='<div class="form-check mb-2 form-check-primary"><input type="checkbox" class="float-start form-check-input chk-parent" id="head_checkbox" onclick=mainCheckBoxSelected(this)>\n<span class="new-control-indicator"></span><span style="visibility:hidden">c</span></div>\n'
      },
      "columnDefs":[
        { 'visible': false, 'targets': [1] },
        {
          "targets":0, "width":"20px", "className":"", "orderable":!1, render:function(e, a, t, n) {
            return'<div class="form-check mb-2 form-check-primary"><input type="checkbox" name="targets_checkbox['+ e + ']" class="float-start form-check-input targets_checkbox" value="' + e + '" onchange=toggleMultipleTargetButton()>\n<span class="new-control-indicator"></span><span style="visibility:hidden">c</span></div>'
          },

        }
      ],
      "order": [[1, 'desc']],
      "dom": "<'dt--top-section'<'row'<'col-12 col-sm-6 d-flex justify-content-sm-start justify-content-center mt-sm-0 mt-3'f><'col-12 col-sm-6 d-flex justify-content-sm-end justify-content-center'l>>>" +
      "<'table-responsive'tr>" +
      "<'dt--bottom-section d-sm-flex justify-content-sm-between text-center'<'dt--pages-count  mb-sm-0 mb-3'i><'dt--pagination'p>>",
      "oLanguage": {
        "oPaginate": { "sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>', "sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>' },
        "sInfo": "Showing page _PAGE_ of _PAGES_",
        "sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
        "sSearchPlaceholder": "Search...",
        "sLengthMenu": "Results :  _MENU_",
      },
      "stripeClasses": [],
      "lengthMenu": [5, 10, 20, 30, 40, 50],
      "pageLength": 20,
      "initComplete": function(settings, json) {
        $('[data-toggle="tooltip"]').tooltip();
        table = settings.oInstance.api();
        var rows = table.rows({
          selected: true
        }).indexes();

        // populate filter menu from datatables
        // populate targets
        var selectedData = table.cells(rows, 2).data();
        var target_array = [];
        for (var i = 0; i < selectedData.length; i++) {
          col1_data = selectedData[i];
          domain_name = col1_data.match(/([^\n]+)/g)[0];
          target_array.push(domain_name);
        }

        target_array = Array.from(new Set(target_array));
        for (target in target_array) {
          select = document.getElementById('filterByTarget');
          var option = document.createElement('option');
          option.value = target_array[target];
          option.innerHTML = target_array[target];
          select.appendChild(option);
        }

        // populate Scan Type
        var selectedData = table.cells(rows, 4).data();
        var scan_type_array = [];
        for (var i = 0; i < selectedData.length; i++) {
          col1_data = extractContent(selectedData[i]);
          scan_type_array.push(col1_data);
        }

        scan_type_array = Array.from(new Set(scan_type_array));
        for (engine in scan_type_array) {
          select = document.getElementById('filterByScanType');
          var option = document.createElement('option');
          option.value = scan_type_array[engine];
          option.innerHTML = scan_type_array[engine];
          select.appendChild(option);
        }
      }
    });
    multiCheck(table);

    // filter organization populate
    $.getJSON(`/api/listOrganizations?&format=json`, function(data) {
      data = data['organizations']
      for (organization in data) {
        name = htmlEncode(data[organization]['name']);
        select = document.getElementById('filterByOrganization');
        var option = document.createElement('option');
        option.value = name;
        option.innerHTML = name;
        select.appendChild(option);
      }
    }).fail(function() {});

    // filtering for scan status
    var status_types = ['Pending', 'Scanning', 'Aborted', 'Successful', 'Failed'];
    for (status in status_types) {
      select = document.getElementById('filterByScanStatus');
      var option = document.createElement('option');
      option.value = status_types[status];
      option.innerHTML = status_types[status];
      select.appendChild(option);
    }

    var org_filter = document.getElementById('filterByOrganization');
    org_filter.addEventListener('click', function() {
      table.search(this.value).draw();
      document.getElementById('filteringText').innerHTML = `<span class="badge badge-soft-primary">Organization: ${this.value}
      <span id="clearFilterChip" class="badge-link" onclick="document.getElementById('resetFilters').click()">X</span>
      </span>`;
      Snackbar.show({
        text: `Filtering by organization ${this.value}`,
        pos: 'top-center'
      });
    }, false);

    var status_filter = document.getElementById('filterByScanStatus');
    status_filter.addEventListener('click', function() {
      table.search(this.value).draw();
      switch (this.value) {
        case 'Pending':
        badge_color = 'warning';
        break;
        case 'Scanning':
        badge_color = 'info';
        break;
        case 'Aborted':
        badge_color = 'danger';
        break;
        case 'Failed':
        badge_color = 'danger';
        break;
        case 'Successful':
        badge_color = 'success';
        break;
        default:
        badge_color = 'primary'
      }
      document.getElementById('filteringText').innerHTML = `<span class="badge badge-soft-${badge_color}">Scan Status: ${this.value}
      <span id="clearFilterChip" class="badge-link" onclick="document.getElementById('resetFilters').click()">X</span>
      </span>`;
      Snackbar.show({
        text: `Filtering by scan status ${this.value}`,
        pos: 'top-center'
      });
    }, false);

    var engine_filter = document.getElementById('filterByScanType');
    engine_filter.addEventListener('click', function() {
      table.search(this.value).draw();
      document.getElementById('filteringText').innerHTML = `<span class="badge badge-soft-primary">Scan Engine: ${this.value}
      <span id="clearFilterChip" class="badge-link" onclick="document.getElementById('resetFilters').click()">X</span>
      </span>`;
      Snackbar.show({
        text: `Filtering by Engine ${this.value}`,
        pos: 'top-center'
      });
    }, false);

    var target_filter = document.getElementById('filterByTarget');
    target_filter.addEventListener('click', function() {
      table.search(this.value).draw();
      document.getElementById('filteringText').innerHTML = `<span class="badge badge-soft-primary">Target/Domain: ${this.value}
      <span id="clearFilterChip" class="badge-link" onclick="document.getElementById('resetFilters').click()">X</span>
      </span>`;
      Snackbar.show({
        text: `Filtering by Engine ${this.value}`,
        pos: 'top-center'
      });
    }, false);

    // reset filtering
    var reset_filter = document.getElementById('resetFilters');
    reset_filter.addEventListener('click', function() {
      resetFilters(table);
    }, false);

  });

  function resetFilters(table_obj) {
    table_obj.search("").draw();
    Snackbar.show({
      text: `Filters Reset`,
      pos: 'top-center'
    });
    document.getElementById('filteringText').innerHTML = '';
  }

  function checkedCount() {
    // this function will count the number of boxes checked
    item = document.getElementsByClassName("targets_checkbox");
    count = 0;
    for (var i = 0; i < item.length; i++) {
      if (item[i].checked) {
        count++;
      }
    }
    return count;
  }


  function toggleMultipleTargetButton() {
    if (checkedCount() > 0) {
      $("#delete_multiple_button").removeClass("disabled");
      $("#stop_multiple_button").removeClass("disabled");
    } else {
      $("#delete_multiple_button").addClass("disabled");
      $("#stop_multiple_button").addClass("disabled");
    }
  }

  function mainCheckBoxSelected(checkbox) {
    if (checkbox.checked) {
      $("#delete_multiple_button").removeClass("disabled");
      $("#stop_multiple_button").removeClass("disabled");
      $(".targets_checkbox").prop('checked', true);
    } else {
      $("#delete_multiple_button").addClass("disabled");
      $("#stop_multiple_button").addClass("disabled");
      $(".targets_checkbox").prop('checked', false);
    }
  }

  function deleteMultipleScan() {
    if (!checkedCount()) {
      swal({
        title: '',
        text: "Oops! You haven't selected any scan to delete.",
        type: 'error',
        padding: '2em'
      })
    } else {
      // atleast one target is selected
      swal.queue([{
        title: 'Are you sure you want to delete ' + checkedCount() + ' Scans?',
        text: "This action is irreversible.\nThis will delete all the scan data and vulnerabilities related to the scan.",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Delete',
        padding: '2em',
        showLoaderOnConfirm: true,
        preConfirm: function() {
          Swal.fire({
          title: 'Deleting...',
          text: 'Please wait while the scans are being deleted.',
          icon: 'info',
          allowOutsideClick: false,
          allowEscapeKey: false,
          showConfirmButton: false
        });
          deleteForm = document.getElementById("scan_history_form");
          deleteForm.action = "../delete/multiple";
          deleteForm.submit();
        }
      }])
    }
  }

  function stopMultipleScan() {
    if (!checkedCount()) {
      swal({
        title: '',
        text: "Oops! You haven't selected any scans to stop.",
        type: 'error',
        padding: '2em'
      })
    } else {
      // atleast one target is selected
      Swal.fire({
        title: 'Are you sure you want to stop ' + checkedCount() + ' Scans?',
        text: "This action is irreversible.\nThis will stop all the selected scans if they are running.",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Stop',
        cancelButtonText: 'Cancel',
        showLoaderOnConfirm: true,
        preConfirm: function() {
          var selected_scan_ids = [];
          $('.targets_checkbox:checked').each(function() {
            selected_scan_ids.push($(this).val());
          });
          data = {
            'scan_ids': selected_scan_ids
          }
          fetch('/api/action/stop/scan/', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'X-CSRFToken': getCookie('csrftoken')
            },
            body: JSON.stringify(data)
          }).then(function(response) {
            if (response.ok) {
              return response.json();
            }
            throw new Error('Network response was not ok.');
          }).then(function(data) {
            if (data['status']) {
              Swal.fire({
                title: 'Success',
                text: data['message'],
                icon: 'success',
                showConfirmButton: false,
                timer: 1500
              });
              setTimeout(function() {
                location.reload();
              }, 1500);
            } else {
              Swal.fire({
                title: 'Error',
                text: data['message'],
                icon: 'error',
                showConfirmButton: false,
                timer: 1500
              });
            }
          }).catch(function(error) {
            Swal.fire({
              title: 'Error',
              text: 'An error occurred while stopping the scans',
              icon: 'error',
              showConfirmButton: false,
              timer: 1500
            });
          });
        }
      });
    }
  }

$('input[name="reportType"]').on('change', function() {
  if (this.value === 'recon') {
    $('input[name="excludeInfoFindings"]').prop('disabled', true);
  } else {
    $('input[name="excludeInfoFindings"]').prop('disabled', false);
  }
});

function initiate_report(id, has_subdomain_scan, has_vulnerability_scan, domain_name) {
  console.log(has_subdomain_scan, has_vulnerability_scan, domain_name);
  //select full report type by default
  $('input[name="reportType"][value="full"]').prop('checked', true);
  $('input[name="excludeInfoFindings"]').prop('checked', true);
  $('#templateSelect').val('modern');
  $('#generateReportModal').modal('show');
  if (has_subdomain_scan) {
    $('input[name="reportType"][value="recon"]').prop('disabled', false);
  } else {
    $('input[name="reportType"][value="recon"]').prop('disabled', true);
  }

  if (has_vulnerability_scan) {
    $('input[name="reportType"][value="vuln"]').prop('disabled', false);
  } else {
    $('input[name="reportType"][value="vuln"]').prop('disabled', true);
  }

  $('#generateReportBtn').attr('onClick', `generate_report(${id}, '${domain_name}')`);
  $('#previewReportBtn').attr('onClick', `preview_report(${id}, '${domain_name}')`);
}

function preview_report(id, domain_name){
  var report_type = $('input[name="reportType"]:checked').val();
  var is_ignore_info_vuln = $('#excludeInfoFindings').is(":checked");
  var report_template = $('#templateSelect').val();
  var url = `/scan/create_report/${id}?report_type=${report_type}`;
  url += `&report_template=${report_template}`;
  if (is_ignore_info_vuln) {
    url += `&ignore_info_vuln`
  }
  $('#generateReportModal').modal('hide');
  window.open(url, '_blank').focus();
}

  function generate_report(id, domain_name) {
    var report_type = $('input[name="reportType"]:checked').val();
    var is_ignore_info_vuln = $('#excludeInfoFindings').is(":checked");
    var report_template = $('#templateSelect').val();
    var url = `/scan/create_report/${id}?report_type=${report_type}&download`;
    url += `&report_template=${report_template}`;
    if (is_ignore_info_vuln) {
      url += `&ignore_info_vuln`
    }
    $('#generateReportModal').modal('hide');
    swal.queue([{
      title: 'Generating Report!',
      text: `Please wait until we generate a report for you!`,
      padding: '2em',
      onOpen: function() {
        swal.showLoading()
        return fetch(url, {
          method: 'POST',
          credentials: "same-origin",
          headers: {
            "X-CSRFToken": getCookie("csrftoken")
          }
        })
        .then(function(response) {
          return response.blob();
        }).then(function(blob) {
          const file = new Blob([blob], {type: 'application/pdf'});
          // process to auto download it
          const fileURL = URL.createObjectURL(file);
          const link = document.createElement('a');
          link.href = fileURL;
          link.download = domain_name + ".pdf";
          link.click();
          swal.close();
        })
        .catch(function() {
          swal.insertQueueStep({
            type: 'error',
            title: 'Oops! Unable to generate report!'
          })
        })
      }
    }]);
  }
</script>
{% endblock page_level_script %}
